{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## map函数"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "source": [
    "from Demos.pipes import cat\n",
    "list1 = [1, 2, 3, 4, 5]\n",
    "r = map(lambda x:x+x, list1)\n",
    "print(list(r))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6, 8, 10]\n"
     ]
    }
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 6, 12, 20, 30]\n"
     ]
    }
   ],
   "source": [
    "m1 = map(lambda x, y: x*x+y, [1, 2, 3, 4, 5], [1, 2, 3, 4, 5])\n",
    "print(list(m1))\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## filter过滤器"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['hello', 'greedy', 'ai']\n"
     ]
    }
   ],
   "source": [
    "def is_not_none(s):\n",
    "    return s and len(s.strip()) >0\n",
    "list2 = [' ', '', 'hello', 'greedy', 'ai']\n",
    "result = filter(is_not_none, list2)\n",
    "print(list(result))\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## reduce函数\n",
    "下次运算用上次运算的结果进行累计运算\n",
    "参数包含初始值"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n"
     ]
    }
   ],
   "source": [
    "from functools import reduce\n",
    "f = lambda x, y :x + y\n",
    "r = reduce(f, [1, 2, 3, 4, 5])\n",
    "print(r)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 列表推导式\n",
    "根据已有的列表推导出新的列表\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6, 8, 10]\n",
      "[1, 8, 27, 64, 125]\n",
      "[4, 5]\n"
     ]
    }
   ],
   "source": [
    "l = [1, 2, 3, 4, 5]\n",
    "l2 = [i +i for i in l]\n",
    "print(l2)\n",
    "l3 = [i**3 for i in l ]\n",
    "print(l3)\n",
    "# 有条件的筛选\n",
    "l4 = [i for i in l if i >3]\n",
    "print(l4)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 集合推导式"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6, 8, 10]\n"
     ]
    }
   ],
   "source": [
    "l = {1, 2, 3, 4, 5}\n",
    "l1 = {i + i for i in l}\n",
    "print(list(l1))\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 字典推导式"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[20, 39, 33]\n",
      "{20: '张三', 39: '李四', 33: '王五'}\n",
      "{'李四': 39}\n"
     ]
    }
   ],
   "source": [
    "s = {\n",
    "    '张三':20,\n",
    "    '李四':39,\n",
    "    '王五':33\n",
    "}\n",
    "# 拿出所有的key值,并变成列表\n",
    "s_keys = [value for key,value in s.items()]\n",
    "print(s_keys)\n",
    "\n",
    "# key和value颠倒\n",
    "s_value_keys = {value:key for key, value in s.items()}\n",
    "print(s_value_keys)\n",
    "\n",
    "# 只拿出符合条件的值\n",
    "s1 = {key:value for key,value in s.items() if key=='李四'}\n",
    "print(s1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 闭包:一个返回值是函数的函数"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1592037236.6459715\n"
     ]
    }
   ],
   "source": [
    "# 调用后打印当前的时间\n",
    "import time\n",
    "def run_time():\n",
    "    def now_time():\n",
    "        print(time.time())\n",
    "    return now_time\n",
    "f = run_time() # f 就相当于是now_time函数\n",
    "f() # 运行now_time函数\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['6,7,8,9,10']\n"
     ]
    }
   ],
   "source": [
    "# 读出文件中带有某个关键字的行\n",
    "def make_filter(keep):  # 接受keep=8\n",
    "    def the_filter(file_name):  # flie_name= data.csv这个文件\n",
    "        file = open('data.csv') # 打开文件\n",
    "        lines = file.readlines()    # 读取文件所有的文件内容\n",
    "        file.close()\n",
    "        filter_doc = [i for i in lines if keep in i]    # 过滤文件内容\n",
    "        return filter_doc\n",
    "    return the_filter\n",
    "filter1 = make_filter('8') # 这一行调用了make_filter函数,接受了the_filter函数作为返回值\n",
    "# 也就是说这里的filter1就等于the_filter\n",
    "result = filter1('data.csv')\n",
    "print(result)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 装饰器, 语法糖,注解"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1592038373.6940236\n",
      "学生跑\n"
     ]
    }
   ],
   "source": [
    "# 获取函数的运行时间\n",
    "import time\n",
    "\n",
    "def run_time(fun):\n",
    "    def get_time():\n",
    "        print(time.time())\n",
    "        fun()   # 这里真正是调用了student_run函数\n",
    "    return get_time\n",
    "\n",
    "@run_time\n",
    "def student_run():\n",
    "    print('学生跑')\n",
    "student_run()   # 调用student_run函数，就是把student_run赋值给\n",
    "# fun\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1592039115.646783\n",
      "学生跑\n",
      "1592039115.64867\n",
      "学生1跑\n",
      "1592039115.64867\n",
      "学生2跑\n"
     ]
    }
   ],
   "source": [
    "# 带参数的装饰器\n",
    "def run_time(fun):\n",
    "    def get_time(*args, **kwargs):\n",
    "        print(time.time())\n",
    "        fun(*args, **kwargs)   # 这里真正是调用了student_run函数\n",
    "    return get_time\n",
    "@run_time\n",
    "def student_run(i):\n",
    "    print('学生跑')\n",
    "@run_time\n",
    "def student_run1(*args):\n",
    "    print('学生1跑')\n",
    "@run_time\n",
    "def student_run2(*args, **kwargs):\n",
    "    print('学生2跑')\n",
    "student_run(1)      # 确定传几个参数的写法\n",
    "student_run1(1, 2)  # 不确定传几个参数的用法，用*args\n",
    "student_run2(1, 2, i=3) # 传入键值对的参数用**kwargs"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "\n"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}